package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysConfig;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.service.SysConfigService;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysConfigController
 * @描述 <br>参数配置相关操作</br>
 * @日期 2019-04-25 17:13
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/config")
@Api(value = "参数配置", description = "参数配置相关操作")
public class SysConfigController extends BaseController {
	private String prefix = "system/config";

	@Autowired
	private SysConfigService configService;

	@RequiresPermissions("system:config:view")
	@GetMapping()
	public String config() {
		return prefix + "/config";
	}

	/**
	 * 查询参数配置列表
	 */
	@RequiresPermissions("system:config:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysConfig config) {
		startPage();
		List<SysConfig> list = configService.selectConfigList(config);
		return getDataTable(list);
	}

	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@ApiOperation(value = "导出", notes = "导出参数配置管理列表数据")
	@RequiresPermissions("system:config:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysConfig config) {
		List<SysConfig> list = configService.selectConfigList(config);
		ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
		return util.exportExcel(list, "参数数据");
	}

	/**
	 * 新增参数配置
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存参数配置
	 */
	@RequiresPermissions("system:config:add")
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@ApiOperation(value = "添加参数", notes = "新增保存参数配置")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysConfig config) {
		config.setCreateUser(ShiroUtils.getLoginName());
		return toResult(configService.insertConfig(config));
	}

	/**
	 * 修改参数配置
	 */
	@GetMapping("/edit/{configId}")
	@ApiOperation(value = "修改参数", notes = "修改参数配置")
	public String edit(@PathVariable("configId") Long configId, ModelMap modelMap) {
		modelMap.put("config", configService.selectConfigById(configId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存参数配置
	 */
	@RequiresPermissions("system:config:edit")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysConfig config) {
		config.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(configService.updateConfig(config));
	}

	/**
	 * 删除参数配置
	 */
	@RequiresPermissions("system:config:remove")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除操作", notes = "删除参数配置")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(configService.deleteConfigByIds(ids));
	}

	/**
	 * 校验参数键名
	 */
	@PostMapping("/checkConfigKeyUnique")
	@ApiOperation(value = "校验参数", notes = "校验参数值是否唯一")
	@ResponseBody
	public String checkConfigKeyUnique(SysConfig config) {
		return configService.checkConfigKeyUnique(config);
	}
}